<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref, watch, onMounted } from 'vue'
import { userRegisterService, userLoginService } from '@/apis/user'
import { useUserStore } from '@/stores/user'
import { useRouter, useRoute } from 'vue-router'

const isRegister = ref(false)
const form = ref()

const UserStore = useUserStore()
const router = useRouter()
const route = useRoute()

onMounted(() => {
  // 显示登录或者是注册页面
  isRegister.value = route.query.isRegister === 'true' ? true : false
})

// 整个的用于提交的form数据对象
const formModel = ref({
  username: '',
  password: '',
  repassword: ''
})

// 整个表单的校验规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    {
      pattern: /^[a-zA-Z0-9_]{5,10}$/,
      message: '用户名必须是5-10位的字符',
      trigger: 'blur'
    }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15位非空字符',
      trigger: 'blur'
    }
  ],
  repassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15位非空字符',
      trigger: 'blur'
    },
    {
      validator: (rule, value, callback) => {
        if (value !== formModel.value.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback() //校验成功也需要callback
        }
      },
      trigger: 'blur'
    }
  ]
}

// 注册账号
const register = async () => {
  await form.value.validate()
  await userRegisterService(formModel.value)
  ElMessage.success('注册成功！')
  isRegister.value = false
}

// 登录账号
const login = async () => {
  await form.value.validate()
  const res = await userLoginService(formModel.value)
  UserStore.setUserList(res.data.user)
  ElMessage.success('登录成功！')
  router.push('/')
}

// 切换的时候，清空表单的内容
watch(isRegister, () => {
  formModel.value = {
    username: '',
    password: '',
    repassword: ''
  }
})
</script>

<template>
  <div class="container">
    <div class="form">
      <!-- 注册页面 -->
      <el-form
        :model="formModel"
        :rules="rules"
        ref="form"
        size="large"
        autocomplete="off"
        v-if="isRegister"
      >
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="formModel.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formModel.password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            v-model="formModel.repassword"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入再次密码"
            @keyup.enter="register"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="register"
            class="button"
            type="primary"
            auto-insert-space
          >
            注册
          </el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link
            type="info"
            :underline="false"
            @click="isRegister = false"
            class="trans"
          >
            <i class="iconfont icon-houtui"></i>&nbsp;登录账号
          </el-link>
        </el-form-item>
      </el-form>

      <!-- 登录页面 -->
      <el-form
        :model="formModel"
        :rules="rules"
        ref="form"
        size="large"
        autocomplete="off"
        v-else
      >
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="formModel.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formModel.password"
            name="password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
            @keyup.enter="login"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="login"
            class="button"
            type="primary"
            auto-insert-space
            >登录</el-button
          >
        </el-form-item>
        <el-form-item class="flex">
          <el-link
            type="info"
            :underline="false"
            @click="isRegister = true"
            class="trans"
          >
            注册账号&nbsp;<i class="iconfont icon-qianjin"></i>
          </el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.container {
  width: 100vw;
  height: 100vh;
  background: url('@/assets/images/background.webp') no-repeat center center;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;

  .form {
    display: flex;
    width: 500px;
    height: 500px;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background-color: #fff;
    border-radius: 50px;
    box-shadow:
      0 10px 20px rgba(0, 0, 0, 0.19),
      0 6px 6px rgba(0, 0, 0, 0.23);
    transition:
      transform 2s ease,
      box-shadow 2s ease;

    &:hover {
      transform: translateY(-5px) scale(1.01); // 垂直位移和轻微缩放
      transition:
        transform 1s ease,
        box-shadow 1s ease; // 仅过渡需要的属性
      box-shadow:
        0 5px 5px rgba(0, 0, 0, 0.5),
        0 2px 2px rgba(0, 0, 0, 0.5); // 改进阴影效果
    }

    .button {
      width: 100%;
      font-weight: bold;
      font-size: 16px;
    }

    .trans {
      font-size: 15px;
    }

    .flex {
      width: 300px;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
